package com.xiang.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiang.reggie.common.BaseContext;
import com.xiang.reggie.common.Result;
import com.xiang.reggie.entity.AddressBook;
import com.xiang.reggie.service.AddressBookService;
import com.xiang.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 半山居雾
 * @date 2022/12/18 14:18
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {


    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @PostMapping()
    public Result<String> save(@RequestBody AddressBook addressBook) {
        //从线程流中获取存储的用户id
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return Result.success("添加地址成功");
    }

    /**
     * 设置默认地址
     *
     * @param addressBook 地址本
     * @return {@code Result<String>}
     */
    @PutMapping("/default")
    public Result<String> updateDefault(@RequestBody AddressBook addressBook) {
        //1.条件构造器   根据同一个用户id的条件  修改该用户下所有地址信息为0
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault, 0);
        //修改更新
        addressBookService.update(updateWrapper);

        //2.将当前需要的地址设为默认1
        addressBook.setIsDefault(1);
        //根据当前地址的id修改该地址状态
        addressBookService.updateById(addressBook);
        return Result.success("设置成功");
    }


    /**
     * 根据ID查询地址
     *
     * @param id id
     * @return {@code Result<AddressBook>}
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable() Long id) {

        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(addressBook);
        } else {
            return Result.error("没有找到该对象");
        }

    }

    /**
     * 查询默认地址
     *
     * @return {@code Result<AddressBook>}
     */
    @GetMapping("/default")
    public Result<AddressBook> getByDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        System.out.println(addressBook);
        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }

    }


    /**
     * 查询指定用户的全部地址
     *
     * @return {@code Result<AddressBook>}
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> queryById() {
        //构造用户id不为null的条件下，通过用户id查询所有的地址信息
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseContext.getCurrentId() != null, AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> bookList = addressBookService.list(queryWrapper);
        return Result.success(bookList);

    }

    @DeleteMapping()
    public Result<String> deleteByIds(Long[] ids) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AddressBook::getId, ids);
        boolean remove = addressBookService.remove(queryWrapper);
        return remove ? Result.success("删除地址成功") : Result.error("删除地址失败");

        /* 第二种
        for (Long id : ids) {
            queryWrapper.eq(AddressBook::getId,id);
            addressBookService.remove(queryWrapper);
        }*/
    }

    /**
     * 修改地址
     *
     * @param addressBook 地址本
     * @return {@code Result<String>}
     */
    @PutMapping()
    public Result<String> update(@RequestBody AddressBook addressBook) {

        addressBookService.updateById(addressBook);

        return Result.success("修改地址成功");
    }


}
